# 博客登陆页面测试
import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

from common.Utils import BlogDriver


class BlogLogin:
    url = ""
    driver = ""
    def __init__(self):
        self.url="http://127.0.0.1:58080/sign-in.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)

    # 成功登录的测试用例
    def LoginSucTest(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("qianzhuo")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("1")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        # 能够找到 首页 俩字，说明登陆成功
        time.sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, "#article_list_board_title")
        # ret = WebDriverWait(self.driver,10).until(
        #     EC.visibility_of_element_located((self.driver.find_element(By.CSS_SELECTOR,"#article_list_board_title")))
        # )
        # assert "首页" in ret

        # 添加屏幕截图
        BlogDriver.getScreeShot()

        # 返回到登陆页面
        # self.driver.back()

    # 异常登录的测试用例
    def LoginFailTest(self):
        # 若连续多次的send_keys则会出现连续的关键词拼接，而不是替换 若要替换，要先clear
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()

        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("qianzhuo")
        # 错误的密码
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123")

        # 所有操作都明确指定等待条件 点击事件也要进行显示等待 否则可能找不到“弹出警告信息”元素
        WebDriverWait(self.driver,10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,"#submit"))
        ).click()

        warning = WebDriverWait(self.driver,10).until(
            # visibility_of_element_located（等待元素出现）  而不是用 invisibility_of_element（等待元素消失）
            EC.visibility_of_element_located((By.XPATH,"/html/body/div[2]/div"))
        )

        # 添加屏幕截图
        BlogDriver.getScreeShot()

        assert "用户名或密码错误" in warning.text


# login = BlogLogin()
# login.LoginFailTest()
# login.LoginSucTest()
# login.driver.quit()

